package com.wt.admin.service.sys.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.admin.domain.dto.sys.DataPermissionsTablesDTO;
import com.wt.admin.domain.entity.sys.SysDataPermissionsEntity;
import com.wt.admin.domain.vo.DataPermissionsFieldsVO;
import com.wt.admin.domain.vo.DataPermissionsTablesVO;
import com.wt.admin.mapper.sys.SysDataPermissionsMapper;
import com.wt.admin.service.sys.SysDataPermissionsService;
import com.wt.admin.util.PageUtil;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class SysDataPermissionsServiceImpl extends ServiceImpl<SysDataPermissionsMapper, SysDataPermissionsEntity> implements SysDataPermissionsService {

    @Resource
    private SysDataPermissionsMapper sysDataPermissionsMapper;


    @Override
    public PageUtil.PageVO<SysDataPermissionsEntity> find(PageUtil.PageDTO<SysDataPermissionsEntity> data) {
        return PageUtil.findPage(data, (page) -> sysDataPermissionsMapper.find(page,data.getData()));
    }

    @Override
    public void edit(SysDataPermissionsEntity data) {
        saveOrUpdate(data);
    }

    @Override
    public void del(SysDataPermissionsEntity data) {
        sysDataPermissionsMapper.deleteById(data);
    }

    @Override
    public List<Object> findByFieldValue(DataPermissionsTablesDTO data) {
        return sysDataPermissionsMapper.findByFieldValue(data);
    }

    @Override
    public List<DataPermissionsTablesVO> findByTables(SysDataPermissionsEntity data) {
        return sysDataPermissionsMapper.findByTables(data);
    }

    @Override
    public List<DataPermissionsFieldsVO> findByFields(SysDataPermissionsEntity data) {
        return sysDataPermissionsMapper.findByFields(data);
    }

    @Override
    public Map<String,String> findRoleIdBydDataPermissions(Integer roleId) {
        List<SysDataPermissionsEntity> list = sysDataPermissionsMapper.findRoleIdBydDataPermissions(roleId);
        return list.stream().collect(
                Collectors.toMap(
                        key -> key.getTableName().toLowerCase(),
                        value -> value.getConditions().toLowerCase()
                )
        );
    }



}
